#if !USE_CONSOLE

#include "src/interface/materialDialog.h"

#include <QtGui>
#include <iostream>

#include "src/math/vector3d.h"

#include "src/defines/material.h"

MaterialDialog::MaterialDialog(GLWidget *_glWidget, QWidget *_parent)
    : QDialog(_parent), inMaterial(false), glWidget(_glWidget){
}

MaterialDialog::~MaterialDialog(){
    delete ambientPushButton;
    delete diffusePushButton;
    delete specularPushButton;
    delete materialTypeComboBox;
}

void MaterialDialog::createMaterialLayout(QVBoxLayout *_mainLayout){
    ambientPushButton = new QPushButton(tr("Cor ambiente"));
    connect(ambientPushButton, SIGNAL(clicked()), this, SLOT(changeAmbient()));

    diffusePushButton = new QPushButton(tr("Cor difusa"));
    connect(diffusePushButton, SIGNAL(clicked()), this, SLOT(changeDiffuse()));

    specularPushButton = new QPushButton(tr("Cor especular"));
    connect(specularPushButton, SIGNAL(clicked()), this, SLOT(changeSpecular()));

    setButtonColor();

    QVBoxLayout *materialLayout = new QVBoxLayout;
    materialLayout->addWidget(ambientPushButton);
    materialLayout->addStretch(1);
    materialLayout->addWidget(diffusePushButton);
    materialLayout->addStretch(1);
    materialLayout->addWidget(specularPushButton);
    materialLayout->addStretch(1);

    QGroupBox *materialGroupBox = new QGroupBox(QString::fromUtf8("Cores do material"));
    materialGroupBox->setLayout(materialLayout);



    QLabel *kaLabel = new QLabel(tr("ka:"));

    QDoubleSpinBox *kaDoubleSpinBox = new QDoubleSpinBox();
    kaDoubleSpinBox->setRange(0.0, 1.0);
    kaDoubleSpinBox->setSingleStep(0.01);
    kaDoubleSpinBox->setValue(ka);
    kaDoubleSpinBox->setDecimals(4);
    connect(kaDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKa(double)));
    connect(this, SIGNAL(setKa(double)), kaDoubleSpinBox, SLOT(setValue(double)));
    connect(kaDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));

    QLabel *kdLabel = new QLabel(tr("kd:"));

    QDoubleSpinBox *kdDoubleSpinBox = new QDoubleSpinBox();
    kdDoubleSpinBox->setRange(0.0, 1.0);
    kdDoubleSpinBox->setSingleStep(0.01);
    kdDoubleSpinBox->setValue(kd);
    kdDoubleSpinBox->setDecimals(4);
    connect(kdDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKd(double)));
    connect(this, SIGNAL(setKd(double)), kdDoubleSpinBox, SLOT(setValue(double)));
    connect(kdDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));

    QLabel *ksLabel = new QLabel(tr("ks:"));

    QDoubleSpinBox *ksDoubleSpinBox = new QDoubleSpinBox();
    ksDoubleSpinBox->setRange(0.0, 1.0);
    ksDoubleSpinBox->setSingleStep(0.01);
    ksDoubleSpinBox->setValue(ks);
    ksDoubleSpinBox->setDecimals(4);
    connect(ksDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKs(double)));
    connect(this, SIGNAL(setKs(double)), ksDoubleSpinBox, SLOT(setValue(double)));
    connect(ksDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));

    QLabel *ktLabel = new QLabel(tr("kt:"));

    QDoubleSpinBox *ktDoubleSpinBox = new QDoubleSpinBox();
    ktDoubleSpinBox->setRange(0.0, 1.0);
    ktDoubleSpinBox->setSingleStep(0.01);
    ktDoubleSpinBox->setValue(kt);
    ktDoubleSpinBox->setDecimals(4);
    connect(ktDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKt(double)));
    connect(this, SIGNAL(setKt(double)), ktDoubleSpinBox, SLOT(setValue(double)));
    connect(ktDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));


    QHBoxLayout *attenuationFactorKaLayout = new QHBoxLayout;
    attenuationFactorKaLayout->addWidget(kaLabel);
    attenuationFactorKaLayout->addWidget(kaDoubleSpinBox);
    attenuationFactorKaLayout->addStretch(1);

    QHBoxLayout *attenuationFactorKdLayout = new QHBoxLayout;
    attenuationFactorKdLayout->addWidget(kdLabel);
    attenuationFactorKdLayout->addWidget(kdDoubleSpinBox);
    attenuationFactorKdLayout->addStretch(1);

    QHBoxLayout *attenuationFactorKsLayout = new QHBoxLayout;
    attenuationFactorKsLayout->addWidget(ksLabel);
    attenuationFactorKsLayout->addWidget(ksDoubleSpinBox);
    attenuationFactorKsLayout->addStretch(1);

    QHBoxLayout *attenuationFactorKtLayout = new QHBoxLayout;
    attenuationFactorKtLayout->addWidget(ktLabel);
    attenuationFactorKtLayout->addWidget(ktDoubleSpinBox);
    attenuationFactorKtLayout->addStretch(1);

    QVBoxLayout *attenuationFactorLayout = new QVBoxLayout;
    attenuationFactorLayout->addLayout(attenuationFactorKaLayout);
    attenuationFactorLayout->addLayout(attenuationFactorKdLayout);
    attenuationFactorLayout->addLayout(attenuationFactorKsLayout);
    attenuationFactorLayout->addLayout(attenuationFactorKtLayout);

    QGroupBox *attenuationFactorGroupBox = new QGroupBox(QString::fromUtf8("Fatores de atenuação"));
    attenuationFactorGroupBox->setLayout(attenuationFactorLayout);


    QDoubleSpinBox *specularPowerDoubleSpinBox = new QDoubleSpinBox();
    specularPowerDoubleSpinBox->setRange(0.0, 1000.0);
    specularPowerDoubleSpinBox->setSingleStep(5);
    specularPowerDoubleSpinBox->setValue(specularPower);
    connect(specularPowerDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeSpecularPower(double)));
    connect(this, SIGNAL(setSpecularPower(double)), specularPowerDoubleSpinBox, SLOT(setValue(double)));
    connect(specularPowerDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));


    QHBoxLayout *specularPowerLayout = new QHBoxLayout;
    specularPowerLayout->addWidget(specularPowerDoubleSpinBox);
    specularPowerLayout->addStretch(1);

    QGroupBox *specularPowerGroupBox = new QGroupBox(QString::fromUtf8("Specular Power"));
    specularPowerGroupBox->setLayout(specularPowerLayout);


    QDoubleSpinBox *refractiveIndexDoubleSpinBox = new QDoubleSpinBox();
    refractiveIndexDoubleSpinBox->setRange(0.0, 10.0);
    refractiveIndexDoubleSpinBox->setSingleStep(0.1);
    refractiveIndexDoubleSpinBox->setDecimals(5);
    refractiveIndexDoubleSpinBox->setValue(refractiveIndex);
    connect(refractiveIndexDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRefractiveIndex(double)));
    connect(this, SIGNAL(setRefractiveIndex(double)), refractiveIndexDoubleSpinBox, SLOT(setValue(double)));
    connect(refractiveIndexDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));


    QHBoxLayout *refractiveIndexLayout = new QHBoxLayout;
    refractiveIndexLayout->addWidget(refractiveIndexDoubleSpinBox);
    refractiveIndexLayout->addStretch(1);

    QGroupBox *refractiveIndexGroupBox = new QGroupBox(QString::fromUtf8("Índice de refração"));
    refractiveIndexGroupBox->setLayout(refractiveIndexLayout);



    texturePushButton = new QPushButton();
    connect(texturePushButton, SIGNAL(clicked()), this, SLOT(changeTexture()));
//    texturePushButton->setEnabled(useTexture);

    setButtonTexture();


    QLabel *repeatTextureLabel = new QLabel(QString::fromUtf8("Repetições :"));
    QLabel *repeatTextureULabel = new QLabel(tr("U:"));

    QSpinBox *repeatTextureUSpinBox = new QSpinBox();
    repeatTextureUSpinBox->setRange(1, 100);
    repeatTextureUSpinBox->setSingleStep(1);
    repeatTextureUSpinBox->setValue(textureRepeatTimesU);
    connect(repeatTextureUSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeTextureRepeatU(int)));
    connect(this, SIGNAL(setRepeatTextureU(int)), repeatTextureUSpinBox, SLOT(setValue(int)));
    connect(repeatTextureUSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeint(int)));

    QHBoxLayout *repeatTextureULayout = new QHBoxLayout;
    repeatTextureULayout->addWidget(repeatTextureULabel);
    repeatTextureULayout->addWidget(repeatTextureUSpinBox);
    repeatTextureULayout->addStretch(1);

    QLabel *repeatTextureVLabel = new QLabel(tr("V:"));

    QSpinBox *repeatTextureVSpinBox = new QSpinBox();
    repeatTextureVSpinBox->setRange(1, 100);
    repeatTextureVSpinBox->setSingleStep(1);
    repeatTextureVSpinBox->setValue(textureRepeatTimesV);
    connect(repeatTextureVSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeTextureRepeatV(int)));
    connect(this, SIGNAL(setRepeatTextureV(int)), repeatTextureVSpinBox, SLOT(setValue(int)));
    connect(repeatTextureVSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeint(int)));

    QHBoxLayout *repeatTextureVLayout = new QHBoxLayout;
    repeatTextureVLayout->addWidget(repeatTextureVLabel);
    repeatTextureVLayout->addWidget(repeatTextureVSpinBox);
    repeatTextureVLayout->addStretch(1);

    QHBoxLayout *repeatTextureHLayout = new QHBoxLayout;
    repeatTextureHLayout->addLayout(repeatTextureULayout);
    repeatTextureHLayout->addLayout(repeatTextureVLayout);
    repeatTextureHLayout->addStretch(1);

    QVBoxLayout *repeatTextureLayout = new QVBoxLayout;
    repeatTextureLayout->addWidget(repeatTextureLabel);
    repeatTextureLayout->addLayout(repeatTextureHLayout);


    QComboBox *textureColoringTypeComboBox = new QComboBox;
    textureColoringTypeComboBox->addItem(QString::fromUtf8("Multiplicar"));
    textureColoringTypeComboBox->addItem(QString::fromUtf8("Substituir"));
    textureColoringTypeComboBox->setCurrentIndex(textureColoringType);
    connect(textureColoringTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeTextureColoringType(int)));

    QComboBox *textureMappingTypeComboBox = new QComboBox;
    textureMappingTypeComboBox->addItem(QString::fromUtf8("Original"));
    textureMappingTypeComboBox->addItem(QString::fromUtf8("Esférica"));
    textureMappingTypeComboBox->addItem(QString::fromUtf8("Planar"));
    textureMappingTypeComboBox->setCurrentIndex(textureMappingType);
    connect(textureMappingTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeTextureMappingType(int)));


    QVBoxLayout *textureLayout = new QVBoxLayout;
    textureLayout->addWidget(texturePushButton);
    textureLayout->addLayout(repeatTextureLayout);
    textureLayout->addWidget(textureColoringTypeComboBox);
    textureLayout->addWidget(textureMappingTypeComboBox);

    QGroupBox *textureGroupBox = new QGroupBox(QString::fromUtf8("Textura"));
    textureGroupBox->setCheckable(true);
//    textureGroupBox->setEnabled(useTexture);
    textureGroupBox->setChecked(useTexture);
    textureGroupBox->setLayout(textureLayout);
    connect(textureGroupBox, SIGNAL(toggled(bool)), this, SLOT(activateTexture(bool)));
    connect(textureGroupBox, SIGNAL(toggled(bool)), textureGroupBox, SLOT(setEnabled(bool)));




    bumpMappingPushButton = new QPushButton();
    connect(bumpMappingPushButton, SIGNAL(clicked()), this, SLOT(changeBumpMapping()));
//    bumpMappingPushButton->setEnabled(useBumpMapping);

    setButtonBumpMapping();

    QLabel *repeatBumpMappingLabel = new QLabel(QString::fromUtf8("Repetições :"));
    QLabel *repeatBumpMappingULabel = new QLabel(tr("U:"));

    QSpinBox *repeatBumpMappingUSpinBox = new QSpinBox();
    repeatBumpMappingUSpinBox->setRange(1, 100);
    repeatBumpMappingUSpinBox->setSingleStep(1);
    repeatBumpMappingUSpinBox->setValue(bumpMappingRepeatTimesU);
    connect(repeatBumpMappingUSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeBumpMappingRepeatU(int)));
    connect(this, SIGNAL(setRepeatTextureU(int)), repeatBumpMappingUSpinBox, SLOT(setValue(int)));
    connect(repeatBumpMappingUSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeint(int)));

    QHBoxLayout *repeatBumpMappingULayout = new QHBoxLayout;
    repeatBumpMappingULayout->addWidget(repeatBumpMappingULabel);
    repeatBumpMappingULayout->addWidget(repeatBumpMappingUSpinBox);
    repeatBumpMappingULayout->addStretch(1);

    QLabel *repeatBumpMappingVLabel = new QLabel(tr("V:"));

    QSpinBox *repeatBumpMappingVSpinBox = new QSpinBox();
    repeatBumpMappingVSpinBox->setRange(1, 100);
    repeatBumpMappingVSpinBox->setSingleStep(1);
    repeatBumpMappingVSpinBox->setValue(bumpMappingRepeatTimesV);
    connect(repeatBumpMappingVSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeBumpMappingRepeatV(int)));
    connect(this, SIGNAL(setRepeatTextureV(int)), repeatBumpMappingVSpinBox, SLOT(setValue(int)));
    connect(repeatBumpMappingVSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeint(int)));

    QHBoxLayout *repeatBumpMappingVLayout = new QHBoxLayout;
    repeatBumpMappingVLayout->addWidget(repeatBumpMappingVLabel);
    repeatBumpMappingVLayout->addWidget(repeatBumpMappingVSpinBox);
    repeatBumpMappingVLayout->addStretch(1);

    QHBoxLayout *repeatBumpMappingHLayout = new QHBoxLayout;
    repeatBumpMappingHLayout->addLayout(repeatBumpMappingULayout);
    repeatBumpMappingHLayout->addLayout(repeatBumpMappingVLayout);
    repeatBumpMappingHLayout->addStretch(1);

    QVBoxLayout *repeatBumpMappingLayout = new QVBoxLayout;
    repeatBumpMappingLayout->addWidget(repeatBumpMappingLabel);
    repeatBumpMappingLayout->addLayout(repeatBumpMappingHLayout);

    QComboBox *bumpMappingMappingTypeComboBox = new QComboBox;
    bumpMappingMappingTypeComboBox->addItem(QString::fromUtf8("Original"));
    bumpMappingMappingTypeComboBox->addItem(QString::fromUtf8("Esférica"));
    bumpMappingMappingTypeComboBox->addItem(QString::fromUtf8("Planar"));
    bumpMappingMappingTypeComboBox->setCurrentIndex(bumpMappingMappingType);
    connect(bumpMappingMappingTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBumpMappingMappingType(int)));


    QLabel *intensityLabel = new QLabel(tr("Intensidade:"));

    QDoubleSpinBox *intensityDoubleSpinBox = new QDoubleSpinBox();
    intensityDoubleSpinBox->setRange(0, 1);
    intensityDoubleSpinBox->setSingleStep(0.1);
    intensityDoubleSpinBox->setDecimals(5);
    intensityDoubleSpinBox->setValue(bumpMappingIntensity);
    connect(intensityDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeBumpMappingIntensity(double)));
    connect(this, SIGNAL(setBumpMappingIntensity(double)), intensityDoubleSpinBox, SLOT(setValue(double)));
    connect(intensityDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(change(double)));

    QHBoxLayout *intensityLayout = new QHBoxLayout;
    intensityLayout->addWidget(intensityLabel);
    intensityLayout->addWidget(intensityDoubleSpinBox);

    QCheckBox *loopCheckBox = new QCheckBox("Loop");
    loopCheckBox->setChecked(bumpMappingLoop);

    QVBoxLayout *bumpMappingLayout = new QVBoxLayout;
    bumpMappingLayout->addWidget(bumpMappingPushButton);
    bumpMappingLayout->addLayout(repeatBumpMappingLayout);
    bumpMappingLayout->addWidget(bumpMappingMappingTypeComboBox);
    bumpMappingLayout->addLayout(intensityLayout);
    bumpMappingLayout->addWidget(loopCheckBox);

    QGroupBox *bumpMappingGroupBox = new QGroupBox(QString::fromUtf8("Bump Mapping"));
    bumpMappingGroupBox->setCheckable(true);
//    bumpMappingGroupBox->setEnabled(useBumpMapping);
    bumpMappingGroupBox->setChecked(useBumpMapping);
    bumpMappingGroupBox->setLayout(bumpMappingLayout);
    connect(bumpMappingGroupBox, SIGNAL(toggled(bool)), this, SLOT(activateBumpMapping(bool)));
//    connect(bumpMappingGroupBox, SIGNAL(toggled(bool)), bumpMappingGroupBox, SLOT(setEnabled(bool)));


//
//
//    QString textureFilePath = QFileDialog::getOpenFileName(this, tr("Abrir cena"), QDir::currentPath(),tr("Arquivo de cenas (*.obs)"));
//
//    if (!filename.isNull()){
//        qDebug() << "abrindo: " << filename;
//        QFile* file = new QFile(filename);
//
//        Input input;
//        Scene *s = newScene(true);
//        input.parseScene(file, s);
//        raytracingDepthSpinBox->setValue(s->getDepth());
//        scenePanel->update(s);
//        cameraPanel->setCamera(canvas->getScene()->getCamera());
//        canvas->updateGL();
//
//        output.setFile(file);
//	}




    //-------------------------------------Materiais prontos
    materialTypeComboBox = new QComboBox;
    materialTypeComboBox->addItem(QString::fromUtf8(CUSTOM));
    materialTypeComboBox->addItem(QString::fromUtf8(BRASS));
    materialTypeComboBox->addItem(QString::fromUtf8(BRONZE));
    materialTypeComboBox->addItem(QString::fromUtf8(POLISHED_BRONZE));
    materialTypeComboBox->addItem(QString::fromUtf8(CHROME));
    materialTypeComboBox->addItem(QString::fromUtf8(COPPER));
    materialTypeComboBox->addItem(QString::fromUtf8(POLISHED_COPPER));
    materialTypeComboBox->addItem(QString::fromUtf8(GOLD));
    materialTypeComboBox->addItem(QString::fromUtf8(POLISHED_GOLD));
    materialTypeComboBox->addItem(QString::fromUtf8(PEWTER));
    materialTypeComboBox->addItem(QString::fromUtf8(SILVER));
    materialTypeComboBox->addItem(QString::fromUtf8(POLISHED_SILVER));
    materialTypeComboBox->addItem(QString::fromUtf8(EMERALD));
    materialTypeComboBox->addItem(QString::fromUtf8(JADE));
    materialTypeComboBox->addItem(QString::fromUtf8(OBSIDIAN));
    materialTypeComboBox->addItem(QString::fromUtf8(PEARL));
    materialTypeComboBox->addItem(QString::fromUtf8(RUBY));
    materialTypeComboBox->addItem(QString::fromUtf8(TURQUOISE));
    materialTypeComboBox->addItem(QString::fromUtf8(BLACK_PLASTIC));
    materialTypeComboBox->addItem(QString::fromUtf8(BLACK_RUBBER));
    materialTypeComboBox->addItem(QString::fromUtf8(GLASS));
    materialTypeComboBox->addItem(QString::fromUtf8(ICE));
    materialTypeComboBox->addItem(QString::fromUtf8(DIAMOND));
    materialTypeComboBox->addItem(QString::fromUtf8(WATER));
    materialTypeComboBox->addItem(QString::fromUtf8(AIR));
    materialTypeComboBox->addItem(QString::fromUtf8(VACUUM));
    connect(materialTypeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeMaterialType(int)));
    connect(this, SIGNAL(changed(int)), materialTypeComboBox, SLOT(setCurrentIndex(int)));

    QHBoxLayout *materialTypeLayout = new QHBoxLayout;
    materialTypeLayout->addWidget(materialTypeComboBox);
    materialTypeLayout->addStretch(1);

    QGroupBox *materialTypeGroupBox = new QGroupBox(QString::fromUtf8("Exemplos de materiais"));
    materialTypeGroupBox->setLayout(materialTypeLayout);


    QVBoxLayout *verticalLayout1 = new QVBoxLayout;
    verticalLayout1->addWidget(materialTypeGroupBox);
    verticalLayout1->addWidget(materialGroupBox);

    QVBoxLayout *verticalLayout2 = new QVBoxLayout;
    verticalLayout2->addWidget(specularPowerGroupBox);
    verticalLayout2->addWidget(refractiveIndexGroupBox);


    QHBoxLayout *horizontalLayout = new QHBoxLayout;
    horizontalLayout->addLayout(verticalLayout1);
    horizontalLayout->addWidget(attenuationFactorGroupBox);
    horizontalLayout->addLayout(verticalLayout2);
    horizontalLayout->addWidget(textureGroupBox);
    horizontalLayout->addWidget(bumpMappingGroupBox);



    _mainLayout->addLayout(horizontalLayout);
//
//    _mainLayout->addWidget(materialTypeGroupBox);
//    _mainLayout->addWidget(materialGroupBox);
//    _mainLayout->addWidget(attenuationFactorGroupBox);
//    _mainLayout->addWidget(specularPowerGroupBox);
//    _mainLayout->addWidget(refractiveIndexGroupBox);
//    _mainLayout->addWidget(textureGroupBox);
}

void MaterialDialog::setButtonTexture(){
    QString textureStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    textureStr.append(QString::fromUtf8("border: 1px solid black;\n"));
    textureStr.append(QString::fromUtf8("background-image: url("));
    textureStr.append(textureFilePath);
    textureStr.append(QString::fromUtf8(");\n"));
    textureStr.append(QString::fromUtf8("border-radius: 8px;\n"));
    textureStr.append(QString::fromUtf8("height: 50px;"));

    texturePushButton->setStyleSheet(textureStr);
}

void MaterialDialog::setButtonBumpMapping(){
    QString bumpMappingStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    bumpMappingStr.append(QString::fromUtf8("border: 1px solid black;\n"));
    bumpMappingStr.append(QString::fromUtf8("background-image: url("));
    bumpMappingStr.append(bumpMappingFilePath);
    bumpMappingStr.append(QString::fromUtf8(");\n"));
    bumpMappingStr.append(QString::fromUtf8("border-radius: 8px;\n"));
    bumpMappingStr.append(QString::fromUtf8("height: 50px;"));

    bumpMappingPushButton->setStyleSheet(bumpMappingStr);
}

void MaterialDialog::setButtonColor(){
    QString ambientStr;
    if(ambientR + ambientB + ambientG < 400){
        ambientStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
        ambientStr.append(QString::fromUtf8("border: 1px solid white;"));
    }else{
        ambientStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
        ambientStr.append(QString::fromUtf8("border: 1px solid black;"));
    }
    ambientStr.append(QString::fromUtf8("background-color: rgb("));
    ambientStr.append(QString::number(ambientR));
    ambientStr.append(QString::fromUtf8(", "));
    ambientStr.append(QString::number(ambientG));
    ambientStr.append(QString::fromUtf8(", "));
    ambientStr.append(QString::number(ambientB));
//    ambientStr.append(QString::fromUtf8(", "));
//    ambientStr.append(QString::number(ambientA));
    ambientStr.append(QString::fromUtf8(");"));
    ambientStr.append(QString::fromUtf8("border-radius: 8px;"));
    ambientStr.append(QString::fromUtf8("height: 20px;"));

    ambientPushButton->setStyleSheet(ambientStr);



    QString diffuseStr;
    if(diffuseR + diffuseB + diffuseG < 400){
        diffuseStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
        diffuseStr.append(QString::fromUtf8("border: 1px solid white;"));
    }else{
        diffuseStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
        diffuseStr.append(QString::fromUtf8("border: 1px solid black;"));
    }
    diffuseStr.append(QString::fromUtf8("background-color: rgb("));
    diffuseStr.append(QString::number(diffuseR));
    diffuseStr.append(QString::fromUtf8(", "));
    diffuseStr.append(QString::number(diffuseG));
    diffuseStr.append(QString::fromUtf8(", "));
    diffuseStr.append(QString::number(diffuseB));
//    diffuseStr.append(QString::fromUtf8(", "));
//    diffuseStr.append(QString::number(diffuseA));
    diffuseStr.append(QString::fromUtf8(");"));
    diffuseStr.append(QString::fromUtf8("border-radius: 8px;"));
    diffuseStr.append(QString::fromUtf8("height: 20px;"));

    diffusePushButton->setStyleSheet(diffuseStr);



    QString specularStr;
    if(specularR + specularB + specularG < 400){
        specularStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
        specularStr.append(QString::fromUtf8("border: 1px solid white;"));
    }else{
        specularStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
        specularStr.append(QString::fromUtf8("border: 1px solid black;"));
    }
    specularStr.append(QString::fromUtf8("background-color: rgb("));
    specularStr.append(QString::number(specularR));
    specularStr.append(QString::fromUtf8(", "));
    specularStr.append(QString::number(specularG));
    specularStr.append(QString::fromUtf8(", "));
    specularStr.append(QString::number(specularB));
//    specularStr.append(QString::fromUtf8(", "));
//    specularStr.append(QString::number(specularA));
    specularStr.append(QString::fromUtf8(");"));
    specularStr.append(QString::fromUtf8("border-radius: 8px;"));
    specularStr.append(QString::fromUtf8("height: 20px;"));

    specularPushButton->setStyleSheet(specularStr);
}


void MaterialDialog::changeAmbient(){
    QColor color = QColorDialog::getColor(QColor(ambientR, ambientG, ambientB/*, ambientA*/), this, "Escolha a cor ambiente", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        ambientR = color.red();
        ambientG = color.green();
        ambientB = color.blue();
//        ambientA = color.alpha();

        change();
    }
    setButtonColor();
}

void MaterialDialog::changeDiffuse(){
    QColor color = QColorDialog::getColor(QColor(diffuseR, diffuseG, diffuseB/*, diffuseA*/), this, "Escolha a cor difusa", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        diffuseR = color.red();
        diffuseG = color.green();
        diffuseB = color.blue();
//        diffuseA = color.alpha();

        change();
    }
    setButtonColor();
}

void MaterialDialog::changeSpecular(){
    QColor color = QColorDialog::getColor(QColor(specularR, specularG, specularB/*, specularA*/), this, "Escolha a cor especular", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        specularR = color.red();
        specularG = color.green();
        specularB = color.blue();
//        specularA = color.alpha();

        change();
    }
    setButtonColor();
}

void MaterialDialog::changeTexture(){

    textureFilePath = QFileDialog::getOpenFileName(this, tr("Escolha a textura"), QDir::currentPath(),tr("Imagens (*.jpg *.png *.gif)"));

    QDir dir(QDir::currentPath());
    textureFilePath = dir.relativeFilePath(textureFilePath);

    setButtonTexture();
}

void MaterialDialog::changeBumpMapping(){

    bumpMappingFilePath = QFileDialog::getOpenFileName(this, tr("Escolha a bump mapping"), QDir::currentPath(),tr("Imagens (*.jpg *.png *.gif)"));

    QDir dir(QDir::currentPath());
    bumpMappingFilePath = dir.relativeFilePath(bumpMappingFilePath);

    setButtonBumpMapping();
}

void MaterialDialog::changeRefractiveIndex(double _ri)
{
    refractiveIndex = _ri;
    if(refractiveIndex != 0)
        ambientA = diffuseA = specularA = 128;
    else
        ambientA = diffuseA = specularA = 255;
}

void MaterialDialog::changeMaterialType(int _type)
{
    inMaterial = true;
    double ambR = 0, ambG = 0, ambB = 0, ambA = 0, dffR = 0, dffG = 0, dffB = 0, dffA = 0, spcR = 0, spcG = 0, spcB = 0, spcA = 0;

    switch(_type)
    {
        case CUSTOM_ID:
            inMaterial = false;
            return;
            break;
        case BRASS_ID:
        {
            ambR = 0.329412;
            ambG = 0.223529;
            ambB = 0.027451;
            ambA = 1.0;
            dffR = 0.780392;
            dffG = 0.568627;
            dffB = 0.113725;
            dffA = 1.0;
            spcR = 0.992157;
            spcG = 0.941176;
            spcB = 0.807843;
            spcA = 1.0;
            specularPower = 27.8974;
            ka = 1;
            kd = 0.8;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 0.0;
            break;
        }
        case BRONZE_ID:
        {
            ambR = 0.2125;
            ambG = 0.1275;
            ambB = 0.054;
            ambA = 1.0;
            dffR = 0.714;
            dffG = 0.4284;
            dffB = 0.18144;
            dffA = 1.0;
            spcR = 0.393548;
            spcG = 0.271906;
            spcB = 0.166721;
            spcA = 1.0;
            specularPower = 25.6;
            ka = 1;
            kd = 0.95;
            ks = 0.05;
            kt = 0.0;
            refractiveIndex = 1.18;
            break;
        }
        case POLISHED_BRONZE_ID:
        {
            ambR = 0.25;
            ambG = 0.148;
            ambB = 0.06475;
            ambA = 1.0;
            dffR = 0.4;
            dffG = 0.2368;
            dffB = 0.1036;
            dffA = 1.0;
            spcR = 0.774597;
            spcG = 0.458561;
            spcB = 0.200621;
            spcA = 1.0;
            specularPower = 76.8;
            ka = 1;
            kd = 0.9;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 1.18;
            break;
        }
        case CHROME_ID:
        {
            ambR = 0.25;
            ambG = 0.25;
            ambB = 0.25;
            ambA = 1.0;
            dffR = 0.4;
            dffG = 0.4;
            dffB = 0.4;
            dffA = 1.0;
            spcR = 0.774597;
            spcG = 0.774597;
            spcB = 0.774597;
            spcA = 1.0;
            specularPower = 76.8;
            ka = 1;
            kd = 0.1;
            ks = 0.9;
            kt = 0.0;
            refractiveIndex = 2.97;//????
            break;
        }
        case COPPER_ID:
        {
            ambR = 0.19125;
            ambG = 0.0735;
            ambB = 0.0225;
            ambA = 1.0;
            dffR = 0.7038;
            dffG = 0.27048;
            dffB = 0.0828;
            dffA = 1.0;
            spcR = 0.256777;
            spcG = 0.137622;
            spcB = 0.086014;
            spcA = 1.0;
            specularPower = 12.8;
            ka = 1;
            kd = 1;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 1.10;
            break;
        }
        case POLISHED_COPPER_ID:
        {
            ambR = 0.2295;
            ambG = 0.08825;
            ambB = 0.0275;
            ambA = 1.0;
            dffR = 0.5508;
            dffG = 0.2118;
            dffB = 0.066;
            dffA = 1.0;
            spcR = 0.580594;
            spcG = 0.223257;
            spcB = 0.0695701;
            spcA = 1.0;
            specularPower = 51.2;
            ka = 1;
            kd = 1;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 1.10;
            break;
        }
        case GOLD_ID:
        {
            ambR = 0.24725;
            ambG = 0.1995;
            ambB = 0.0745;
            ambA = 1.0;
            dffR = 0.75164;
            dffG = 0.60648;
            dffB = 0.22648;
            dffA = 1.0;
            spcR = 0.628281;
            spcG = 0.555802;
            spcB = 0.366065;
            spcA = 1.0;
            specularPower = 51.2;
            ka = 1;
            kd = 1;
            ks = 0.05;
            kt = 0.0;
            refractiveIndex = 0.47;//???
            break;
        }
        case POLISHED_GOLD_ID:
        {
            ambR = 0.24725;
            ambG = 0.2245;
            ambB = 0.0645;
            ambA = 1.0;
            dffR = 0.34615;
            dffG = 0.3143;
            dffB = 0.0903;
            dffA = 1.0;
            spcR = 0.797357;
            spcG = 0.723991;
            spcB = 0.208006;
            spcA = 1.0;
            specularPower = 83.2;
            ka = 1;
            kd = 1;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 0.47;//???
            break;
        }
        case PEWTER_ID:
        {
            ambR = 0.105882;
            ambG = 0.058824;
            ambB = 0.113725;
            ambA = 1.0;
            dffR = 0.427451;
            dffG = 0.470588;
            dffB = 0.541176;
            dffA = 1.0;
            spcR = 0.333333;
            spcG = 0.333333;
            spcB = 0.521569;
            spcA = 1.0;
            specularPower = 9.84615;
            ka = 1;
            kd = 1;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 0.0;
            break;
        }
        case SILVER_ID:
        {
            ambR = 0.19225;
            ambG = 0.19225;
            ambB = 0.19225;
            ambA = 1.0;
            dffR = 0.50754;
            dffG = 0.50754;
            dffB = 0.50754;
            dffA = 1.0;
            spcR = 0.508273;
            spcG = 0.508273;
            spcB = 0.508273;
            spcA = 1.0;
            specularPower = 51.2;
            ka = 1;
            kd = 1;
            ks = 0.08;
            kt = 0.0;
            refractiveIndex = 0.18;
            break;
        }
        case POLISHED_SILVER_ID:
        {
            ambR = 0.23125;
            ambG = 0.23125;
            ambB = 0.23125;
            ambA = 1.0;
            dffR = 0.2775;
            dffG = 0.2775;
            dffB = 0.2775;
            dffA = 1.0;
            spcR = 0.773911;
            spcG = 0.773911;
            spcB = 0.773911;
            spcA = 1.0;
            specularPower = 89.6;
            ka = 1;
            kd = 1;
            ks = 0.1;
            kt = 0.0;
            refractiveIndex = 0.18;//???
            break;
        }
        case EMERALD_ID:
        {
            ambR = 0.0215;
            ambG = 0.1745;
            ambB =  0.0215;
            ambA = 1.0;
            dffR = 0.07568;
            dffG = 0.61424;
            dffB = 0.07568;
            dffA = 1.0;
            spcR = 0.633;
            spcG =  0.727811;
            spcB =  0.633;
            spcA = 1.0;
            specularPower = 76.8;
            ka = 0.55;//???
            kd = 0.55;//???
            ks = 0.1;//???
            kt = 0.45;//???
            refractiveIndex = 1.570;
            break;
        }
        case JADE_ID:
        {
            ambR = 0.135;
            ambG = 0.2225;
            ambB = 0.1575;
            ambA = 1.0;
            dffR = 0.54;
            dffG = 0.89;
            dffB = 0.63;
            dffA = 1.0;
            spcR = 0.316228;
            spcG = 0.316228;
            spcB = 0.316228;
            spcA = 1.0;
            specularPower = 12.8;
            ka = 0.95;//???
            kd = 0.95;//???
            ks = 0.05;//???
            kt = 0.05;//???
            refractiveIndex = 1.65;
            break;
        }
        case OBSIDIAN_ID:
        {
            ambR = 0.05375;
            ambG = 0.05;
            ambB = 0.06625;
            ambA = 1.0;
            dffR = 0.18275;
            dffG = 0.17;
            dffB = 0.22525;
            dffA = 1.0;
            spcR = 0.332741;
            spcG = 0.328634;
            spcB = 0.346435;
            spcA = 1.0;
            specularPower = 38.4;
            ka = 0.82;//???
            kd = 0.82;//???
            ks = 0.08;//???
            kt = 0.18;//???
            refractiveIndex = 1.489;
            break;
        }
        case PEARL_ID:
        {
            ambR = 0.25;
            ambG = 0.20725;
            ambB = 0.20725;
            ambA = 1.0;
            dffR = 1.0;
            dffG = 0.829;
            dffB = 0.829;
            dffA = 1.0;
            spcR = 0.296648;
            spcG = 0.296648;
            spcB = 0.296648;
            spcA = 1.0;
            specularPower = 11.264;
            ka = 0.922;//???
            kd = 0.922;//???
            ks = 0.2;//???
            kt = 0.0;//0.078;//???
            refractiveIndex = 1.530;
            break;
        }
        case RUBY_ID:
        {
            ambR = 0.1745;
            ambG = 0.01175;
            ambB = 0.01175;
            ambA = 1.0;
            dffR = 0.61424;
            dffG = 0.04136;
            dffB = 0.04136;
            dffA = 1.0;
            spcR = 0.727811;
            spcG = 0.626959;
            spcB = 0.626959;
            spcA = 1.0;
            specularPower = 76.8;
            ka = 0.55;//???
            kd = 0.55;//???
            ks = 0.15;//???
            kt = 0.45;//???
            refractiveIndex = 1.77;
            break;
        }
        case TURQUOISE_ID:
        {
            ambR = 0.1;
            ambG = 0.18725;
            ambB = 0.1745;
            ambA = 1.0;//0.8
            dffR = 0.396;
            dffG = 0.74151;
            dffB = 0.69102;
            dffA = 1.0;//0.8
            spcR = 0.297254;
            spcG = 0.30829;
            spcB = 0.306678;
            spcA = 1.0;//0.8
            specularPower = 12.8;
            ka = 0.8;//???
            kd = 0.8;//???
            ks = 0.1;//???
            kt = 0.0;//0.2;//???
            refractiveIndex = 1.610;
            break;
        }
        case BLACK_PLASTIC_ID:
        {
            ambR = 0;
            ambG = 0;
            ambB = 0;
            ambA = 1;
            dffR = 0.01;
            dffG = 0.01;
            dffB = 0.01;
            dffA = 1;
            spcR = 0.5;
            spcG = 0.5;
            spcB = 0.5;
            spcA = 1;
            specularPower = 32;
            ka = 1;
            kd = 0.8;
            ks = 0.5;
            kt = 0.01;//??
            refractiveIndex = 1.46;//??
            break;
        }
        case BLACK_RUBBER_ID:
        {
            ambR = 0.02;
            ambG = 0.02;
            ambB = 0.02;
            ambA = 1;
            dffR = 0.01;
            dffG = 0.01;
            dffB = 0.01;
            dffA = 1;
            spcR = 0.4;
            spcG = 0.4;
            spcB = 0.4;
            spcA = 1;
            specularPower = 10;
            ka = 1;
            kd = 1;
            ks = 0.01;
            kt = 0.0;//???
            refractiveIndex = 1.5191;//???
            break;
        }
        case GLASS_ID:
        {
            ambR = 0.0;
            ambG = 0.0;
            ambB = 0.0;
            ambA = 0.0;
            dffR = 0.0;
            dffG = 0.0;
            dffB = 0.0;
            dffA = 0.0;
            spcR = 0.0;
            spcG = 0.0;
            spcB = 0.0;
            spcA = 0.0;
            specularPower = 100;
            ka = 1;
            kd = 0;
            ks = 0.7;
            kt = 0.95;
            refractiveIndex = 1.5;
            break;
        }
        case ICE_ID:
        {
            ambR = 0.0;
            ambG = 0.0;
            ambB = 0.0;
            ambA = 0.0;
            dffR = 0.0;
            dffG = 0.0;
            dffB = 0.0;
            dffA = 0.0;
            spcR = 0.0;
            spcG = 0.0;
            spcB = 0.0;
            spcA = 0.0;
            specularPower = 100;
            ka = 1;
            kd = 0;
            ks = 0.7;
            kt = 0.95;
            refractiveIndex = 1.3;
            break;
        }
        case DIAMOND_ID:
        {
            ambR = 0.0;
            ambG = 0.0;
            ambB = 0.0;
            ambA = 0.0;
            dffR = 0.0;
            dffG = 0.0;
            dffB = 0.0;
            dffA = 0.0;
            spcR = 0.0;
            spcG = 0.0;
            spcB = 0.0;
            spcA = 0.0;
            specularPower = 100;
            ka = 1;
            kd = 0;
            ks = 0.9;
            kt = 0.95;
            refractiveIndex = 2.42;
            break;
        }
        case WATER_ID:
        {
            ambR = 0.0;
            ambG = 0.0;
            ambB = 0.0001;
            ambA = 0.0;
            dffR = 0.0;
            dffG = 0.0;
            dffB = 0.0001;
            dffA = 0.0;
            spcR = 0.0;
            spcG = 0.0;
            spcB = 0.0001;
            spcA = 0.0;
            specularPower = 1;
            ka = 1;
            kd = 0.1;
            ks = 0.9;
            kt = 0.95;
            refractiveIndex = 1.33;
            break;
        }
        case AIR_ID:
        {
            ambR = 0.0;
            ambG = 0.0;
            ambB = 0.0;
            ambA = 0.0;
            dffR = 0.0;
            dffG = 0.0;
            dffB = 0.0;
            dffA = 0.0;
            spcR = 0.0;
            spcG = 0.0;
            spcB = 0.0;
            spcA = 0.0;
            specularPower = 1;
            ka = 1;
            kd = 0;
            ks = 1;
            kt = 0.95;
            refractiveIndex = 2.42;
            break;
        }
        case VACUUM_ID:
        {
            ambR = 0.0;
            ambG = 0.0;
            ambB = 0.0;
            ambA = 0.0;
            dffR = 0.0;
            dffG = 0.0;
            dffB = 0.0;
            dffA = 0.0;
            spcR = 0.0;
            spcG = 0.0;
            spcB = 0.0;
            spcA = 0.0;
            specularPower = 10;
            ka = 1;
            kd = 0;
            ks = 1;
            kt = 0.95;
            refractiveIndex = 2.42;
            break;
        }
    }


    ambientR = ambR * 255.0;
    ambientG = ambG * 255.0;
    ambientB = ambB * 255.0;
    ambientA = ambA * 255.0;
    diffuseR = dffR * 255.0;
    diffuseG = dffG * 255.0;
    diffuseB = dffB * 255.0;
    diffuseA = dffA * 255.0;
    specularR = spcR * 255.0;
    specularG = spcG * 255.0;
    specularB = spcB * 255.0;
    specularA = spcA * 255.0;
    emit setSpecularPower(specularPower);
    emit setKa(ka);
    emit setKd(kd);
    emit setKs(ks);
    emit setKt(kt);
    emit setRefractiveIndex(refractiveIndex);

    setButtonColor();
    inMaterial = false;
}
#endif //!USE_CONSOLE
